/**
 * Project: complex.cs
 * 
 * File Created at 2010-7-1
 * $Id$
 * 
 * Copyright 2008 Alibaba.com Croporation Limited.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * Alibaba Company. ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with Alibaba.com.
 */
package mina;

import org.apache.mina.common.IoHandlerAdapter;
import org.apache.mina.common.IoSession;

/**
 * 自定议的消息处理器,必须实现IoHandlerAdapter类
 * 
 */
public class SamplMinaServerHandler extends IoHandlerAdapter {
    //当一个客端端连结进入时
    @Override
    public void sessionOpened(IoSession session) throws Exception {
        System.out.println("incomming client : " + session.getRemoteAddress());
    }

    //当一个客户端关闭时
    @Override
    public void sessionClosed(IoSession session) {
        System.out.println("one Clinet Disconnect !");
    }

    //当客户端发送的消息到达时:
    @Override
    public void messageReceived(IoSession session, Object message) throws Exception {
        //我们己设定了服务器解析消息的规则是一行一行读取,这里就可转为String:
        String s = (String) message;
        // Write the received data back to remote peer
        System.out.println("收到客户机发来的消息: " + s);
        //测试将消息回送给客户端
        session.write(s + count);
        count++;
    }

    private int count = 0;
}
